package com.pt25.service.sys.application.impl;

import com.pt25.base.dep.db.DbConstants;
import com.pt25.base.exception.BizException;
import com.pt25.base.util.JsonUtil;
import com.pt25.base.util.StrUtil;
import com.pt25.service.sys.application.ConfigApp;
import com.pt25.service.sys.domain.config.ddo.ConfigData;
import com.pt25.service.sys.domain.config.ddo.ConfigLog;
import com.pt25.service.sys.domain.config.ddo.ConfigType;
import com.pt25.service.sys.domain.config.ddo.ConfigValue;
import com.pt25.service.sys.domain.config.service.ConfigDataService;
import com.pt25.service.sys.domain.config.service.ConfigLogService;
import com.pt25.service.sys.domain.config.service.ConfigTypeService;
import com.pt25.service.sys.domain.config.service.ConfigValueService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class ConfigAppImpl implements ConfigApp {

    @Resource
    private ConfigTypeService configTypeService;
    @Resource
    private ConfigDataService configDataService;
    @Resource
    private ConfigValueService configValueService;
    @Resource
    private ConfigLogService configLogService;

    @Override
    public Boolean delConfigType(Long id) {
        long configDataCount = configDataService.getConfigDataCount(id);
        if (configDataCount > 0) {
            log.warn("还存在configData数据，不可以删除configType,id={}!", id);
            throw new BizException("还存在configData数据，不可以删除configType!");
        }
        return configTypeService.delConfigType(id);
    }

    @Override
    public Long addConfigData(ConfigData configData) {
        ConfigType configType = configTypeService.getConfigType(configData.getConfigTypeId());
        return configDataService.addConfigData(configData, configType);
    }

    @Override
    public Boolean updateConfigData(ConfigData configData) {
        long configValueCount = configValueService.getConfigValueCount(configData.getId());
        if (configValueCount > 0
                && (StrUtil.isBlank(configData.getDataType())
                || StrUtil.isBlank(configData.getDataKey())
                || StrUtil.isBlank(configData.getValueType()))) {
            log.warn("已经存在configValue数据,dataType、dataKey和valueType不可以修改,configData={}！", JsonUtil.toJson(configData));
            throw new BizException("已经存在configValue数据,dataType、dataKey和valueType不可以修改！");
        }
        if (null == configData.getConfigTypeId()) {
            ConfigData oldConfigData = configDataService.getConfigData(configData.getId());
            if (null == oldConfigData) {
                log.warn("配置数据不存在,id={}!", configData.getId());
                throw new BizException("配置数据不存在！");
            }
            configData.setConfigTypeId(oldConfigData.getConfigTypeId());
        }
        ConfigType configType = configTypeService.getConfigType(configData.getConfigTypeId());
        return configDataService.updateConfigData(configData, configType);
    }

    @Override
    public Boolean delConfigData(Long id) {
        long configValueCount = configValueService.getConfigValueCount(id);
        if (configValueCount > 0) {
            log.warn("还存在configValue数据，不可以删除configData,id={}!", id);
            throw new BizException("还存在configValue数据，不可以删除configData!");
        }
        return configDataService.delConfigData(id);
    }

    @Override
    @Transactional
    public Long addConfigValue(ConfigValue configValue) {
        ConfigData configData = configDataService.getConfigData(configValue.getConfigDataId());
        long id = configValueService.addConfigValue(configValue, configData);
        if (id > 0) {
            long logId = configLogService.addConfigLog(configData, configValueService.getConfigValue(id), "add");
            if (logId <= 0) {
                log.warn("add-configLog版本数据保存失败,confiValue={}！", JsonUtil.toJson(configValue));
                throw new BizException("add-configLog版本数据保存失败！");
            }
        }
        return id;
    }

    @Override
    public Boolean delConfigValue(long id, Object valueContent) {
        ConfigValue configValue = configValueService.getConfigValue(id);
        if (configValue == null) {
            log.warn("配置数据不存在,id={},valueContent={}！", id, JsonUtil.toJson(valueContent));
            throw new BizException("配置数据不存在！");
        }
        ConfigData configData = configDataService.getConfigData(configValue.getConfigDataId());
        return configValueService.delConfigValue(configValue, valueContent, configData);
    }

    @Override
    @Transactional
    public Boolean updateConfigValue(ConfigValue configValue) {
        ConfigValue oldConfigValue = configValueService.getConfigValue(configValue.getId());
        if (oldConfigValue == null) {
            log.warn("配置数据不存在,configValue={}！", JsonUtil.toJson(configValue));
            throw new BizException("配置数据不存在！");
        }
        ConfigData configData = configDataService.getConfigData(configValue.getConfigDataId());
        Boolean result = configValueService.updateConfigValue(configValue, configData);
        if (result) {
            long logId = configLogService.addConfigLog(configData, configValueService.getConfigValue(configValue.getId()), "update");
            if (logId <= 0) {
                log.warn("update-configLog版本数据保存失败,confiValue={}！", JsonUtil.toJson(configValue));
                throw new BizException("update-configLog版本数据保存失败！");
            }
        }
        return result;
    }

    @Override
    @Transactional
    public Boolean rollbackConfigValueByConfigLog(long configLogId) {
        ConfigLog configLog = configLogService.getConfigLog(configLogId);
        ConfigValue configValue = configValueService.getConfigValue(configLog.getConfigValueId());
        if (null != configValue) {
            configValue.setValueKey(configLog.getValueKey());
            configValue.setValueContent(configLog.getValueContent());
            configValue.setDelFlag(DbConstants.DEL_FLAG0);
            ConfigData configData = configDataService.getConfigData(configValue.getConfigDataId());
            Boolean result = configValueService.updateConfigValue(configValue, configData);
            if (result) {
                long logId = configLogService.addConfigLog(configData, configValueService.getConfigValue(configValue.getId()), "rollback");
                if (logId <= 0) {
                    log.warn("rollback-configLog版本数据保存失败,configLogId{}！", configLogId);
                    throw new BizException("rollback-configLog版本数据保存失败！");
                }
            }
            return result;
        }
        return false;
    }


}
